home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MIDIMOD2.ZIP / MIDIMOD.C < prev    next >
Text File  |  1993-06-28  |  41KB  |  1,599 lines

  1. /*
  2.  * MIDIMOD.C - Amiga Module to MIDI file converter
  3.  * (c)opyright Andrew Scott 1993
  4.  *
  5.  * Turbo C 2.0
  6.  *
  7.  * Description: Takes a .mod file and has a good go at converting it to
  8.  *              a .mid file. Equivalents to certain .mod samples can be
  9.  *              set to default to particular MIDI instruments. Multiple
  10.  *              MIDI instrument tables should be supported. Note that .mod
  11.  *              and .mid extensions are at the end of the filename.
  12.  *
  13.  * Author: Andrew Scott (Adrenalin Software)
  14.  *
  15.  * Date: 14/3/1993 ver 0.1
  16.  *       20/4/1993 ver 0.2
  17.  */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <math.h>   /* don't forget to link in appropriate libraries */
  23.  
  24. #include "textwin.h" /* Simple text-windowing environment */
  25. #include "midimod.h" /* Dialog/Info box messages and definitions */
  26.  
  27. bfile MidFile, ModFile;
  28. char SongName[21];
  29. samps Samples;
  30. unsigned long PosLog[64];
  31. int DrumChann = 9, TempoType = 1, PosI = 0;
  32. string MidFN, ModFN;
  33.  
  34. void OutByte(bfile f, char b)
  35. /* Post: The byte b has been written to the buffer of the file f */
  36. {
  37.     if (f->o == BUFFSIZE) {
  38.         fwrite(f->b, 1, BUFFSIZE, f->f);
  39.         f->o = 0;
  40.   }
  41.     f->b[f->o++] = b;
  42. }
  43.  
  44. void FlushOut(bfile f)
  45. /* Pre: f was opened for writing */
  46. /* Post: The file f has has its buffer flushed */
  47. {
  48.     if (f->o > 0)
  49.         fwrite(f->b, 1, f->o, f->f);
  50.     f->o = 0;
  51. }
  52.  
  53. void CloseOut(bfile f)
  54. /* Pre: f was opened for writing */
  55. /* Post: The file f has been flushed and is now closed */
  56. {
  57.     FlushOut(f);
  58.     fclose(f->f);
  59.     f->f = NULL;
  60. }
  61.  
  62. int OpenOut(bfile f, string fn)
  63. /* Returns: NZ if the file f has been opened for writing with the name fn */
  64. {
  65.     if (f->f != NULL)
  66.         CloseOut(f);
  67.     f->f = fopen(fn, "wb");
  68.     f->o = 0;
  69.     return f->f != NULL;
  70. }
  71.  
  72. unsigned long Beatle(bfile f)
  73. /* Returns: bfile-tell (btell=beatle). The offset from the start */
  74. {
  75.     return ftell(f->f) + f->o;
  76. }
  77.  
  78. unsigned char InByte(bfile f)
  79. /* Pre: f was opened for reading */
  80. /* Returns: The next byte from the file f */
  81. {
  82.     if (f->o == BUFFSIZE && !feof(f->f)) {
  83.         f->r = fread(f->b, 1, BUFFSIZE, f->f);
  84.         if (f->r < BUFFSIZE)
  85.             f->b[f->r] = 0;
  86.         f->o = 0;
  87.     }
  88.     if (f->o < f->r)
  89.          return f->b[f->o++];
  90.     return f->b[f->o];
  91. }
  92.  
  93. void CloseIn(bfile f)
  94. /* Post: The file f is now closed */
  95. {
  96.     fclose(f->f);
  97.     f->f = NULL;
  98. }
  99.  
  100. int OpenIn(bfile f, string fn)
  101. /* Returns: NZ if the file f has been opened for reading with the name fn */
  102. {
  103.     if (f->f != NULL)
  104.         CloseIn(f);
  105.     f->f = fopen(fn, "rb");
  106.     f->o = f->r = BUFFSIZE;
  107.     return f->f != NULL;
  108. }
  109.  
  110. void Inskipp(bfile f, unsigned long n) /* Stainless-steel rat for Pres */
  111. /* Pre: f was opened for reading */
  112. /* Post: f's file pointer has skipped forward n bytes */
  113. {
  114.     n += f->o;
  115.     while (n >= BUFFSIZE && !feof(f->f)) {
  116.         f->r = fread(f->b, 1, BUFFSIZE, f->f);
  117.         if (f->r < BUFFSIZE)
  118.             f->b[f->r] = 0;
  119.         n -= BUFFSIZE;
  120.     }
  121.     f->o = n; /* hmmm.. may cause an error if was eof.. X-fingers */
  122. }
  123.  
  124. struct bpos FPos(bfile f)
  125. /* Returns: All necessary information regarding file f's status */
  126. {
  127.     struct bpos x;
  128.  
  129.     x.d = *f;
  130.     x.p = ftell(f->f);
  131.     return x;
  132. }
  133.  
  134. void FGoto(bfile f, struct bpos x)
  135. /* Pre: x was the status of f previously */
  136. /* Post: File f has had its status changed to x */
  137. {
  138.     fseek(f->f, x.p, SEEK_SET);
  139.     *f = x.d;
  140. }
  141.  
  142. int WriteVLQ(bfile f, unsigned long i)
  143. /*
  144.  * Returns: # of bytes written after a variable-length-quantity equivalent
  145.  *    of i has been written to the file f.
  146.  */
  147. {
  148.     int x = 0;
  149.     unsigned long buffer;
  150.  
  151.     buffer = i & 127;
  152.     while ((i >>= 7) > 0)
  153.         buffer = ((buffer << 8) | 128) + (i & 127);
  154.     while (1) {
  155.         OutByte(f, buffer & 255);
  156.         x++;
  157.         if (buffer & 128)
  158.             buffer >>= 8;
  159.         else
  160.             return x;
  161.     }
  162. }
  163.  
  164. string InitFile(bfile f, string deffn, string t, int inpm)
  165. /* Returns: NULL if file is unacceptable, the filename otherwise. The
  166.  *    filename corresponds to file f, type t, with default filename deffn.
  167.  *    Will open an input file if inpm is NZ. deffn will be freed.
  168.  */
  169. {
  170.     int r = 0;
  171.     string newfn;
  172.  
  173.     _IF[1] = t;
  174.     if (deffn==NULL)
  175.         *(deffn = (string) malloc(1)) = 0;
  176.     newfn = DialogBox(_IF, deffn);
  177.     free(deffn);
  178.     if (! *newfn) {
  179.         free(newfn);
  180.         return NULL;
  181.     }
  182.     if (inpm)
  183.         r = OpenIn(f, newfn);
  184.     else if (fclose(fopen(newfn, "r"))==EOF || tolower(InfoBox(_OUTE))=='y')
  185.         r = OpenOut(f, newfn);
  186.     if (!r) {
  187.         free(newfn);
  188.         return NULL;
  189.     }
  190.     return newfn;
  191. }
  192.  
  193. int MKTest(bfile f)
  194. /* Returns: The number of samples in the Module file f */
  195. {
  196.     unsigned long offset;
  197.     int i = 15;
  198.     char s[4];
  199.  
  200.     offset = ftell(f->f);
  201.     if (!fseek(f->f, 1080, SEEK_SET)) {
  202.         fread(s, 1, 4, f->f);
  203.         if (!memcmp(s,"M.K.",4) || !memcmp(s,"M!K!",4) || !memcmp(s,"FLT4",4))
  204.             i = 31;
  205.         else if (!memcmp(s,"FLT8",4))
  206.             i = 0;
  207.     }
  208.     fseek(f->f, offset, SEEK_SET);
  209.     return i;
  210. }
  211.  
  212. string SimplifyName(string s)
  213. /*
  214.  * Returns: A string similar to s, but has had any nasty headers removed
  215.  *    any leading spaces or trailing spaces, and all made lower-case with
  216.  *    any intermediate spaces replaced by underbar-characters
  217.  */
  218. {
  219.     string t, r, x;
  220.  
  221.     x = strchr(s, ':');
  222.     if (x != NULL && x-s == 5 && tolower(s[0])=='s' && tolower(s[1])=='t' &&
  223.      s[2]=='-') {
  224.         r = x = (string) malloc(18);
  225.         t = s + 6;
  226.         while (*(x++) = *(t++)); /* remove soundtracker header */
  227.     } else
  228.         strcpy(r = (string) malloc(1+strlen(s)), s); /* strdup? */
  229.     for (t = r; *t == ' '; t++);
  230.     x = r;
  231.     while (*(x++) = *(t++)); /* remove leading spaces */
  232.     if (*r) {
  233.         x--;
  234.         while (*(--x) == ' ') /* remove trailing spaces */
  235.             *x = 0;
  236.     }
  237.     for (x = r; *x; x++) /* fill intermediate spaces with _ */
  238.         if (*x == ' ')
  239.             *x = '_';
  240.     t = r = (string) realloc(r, strlen(r) + 1);
  241.     for (; *t; t++)
  242.         *t = tolower(*t); /* make the lot lower-case */
  243.     return r;
  244. }
  245.  
  246. string GetLine(FILE *f)
  247. /* Returns: Next line from file f, NULL on error/eof */
  248. {
  249.     string s, t;
  250.     int c;
  251.  
  252.     if ((t = s = (string) malloc(MAXSTRING))==NULL) {
  253.         InfoBox(_OOME);
  254.         return NULL;
  255.     }
  256.     while ((c = fgetc(f)) != EOF && c != '\n')
  257.         *(t++) = c;
  258.     if (s == t) {
  259.         free(s);
  260.         return NULL;
  261.     }
  262.     *t = 0;
  263.     return (string) realloc(s, t-s+1);
  264. }
  265.  
  266. int ReadModSpecs(bfile f, string n, samps s)
  267. /*
  268.  * Returns: Z if f is not a supported Amiga Module, else n is set to
  269.  * be the name of the Module and s is set to hold sample information
  270.  */
  271. {
  272.     unsigned char b, c;
  273.     int i, okmodule;
  274.     string t1;
  275.     samp *t2;
  276.  
  277.     for (i = 20, t1 = n; i--; *(t1++) = InByte(f));
  278.     okmodule = !*n || isprint(*n);
  279.     *t1 = 0;
  280.     c = s->n = MKTest(f);
  281.     okmodule = okmodule && c;
  282.     for (t2 = s->s; c--; t2++) {
  283.         for (i = 22, t1 = t2->n; i--; *(t1++) = InByte(f));
  284.         okmodule = okmodule && (!*(t2->n) || isprint(*(t2->n)));
  285.         *t1 = 0;
  286.         b = InByte(f);
  287.         t2->l = 256 * b + InByte(f);
  288.         if (t2->l < 4)
  289.             t2->l = 0;    /* if the sample is this small, not worth processing */
  290.         b = InByte(f);
  291.         t2->v = InByte(f);
  292.         InByte(f);
  293.         InByte(f);
  294.         b = InByte(f);
  295.         if (256 * b + InByte(f) > 1 && t2->l)
  296.             t2->l = -1;    /* looping: plays 'forever' */
  297.         t2->m = 0;
  298.     }
  299.     return !feof(f->f) && okmodule;
  300. }
  301.  
  302. void ScanSamples(samps s)
  303. /*
  304.  * Post: Sample data for ModFile has been scanned and samples in s have
  305.  * been given "reasonable" values for volume shifting and transpositions
  306.  * I hope :)
  307.  */
  308. {
  309.     samp *sam;
  310.     unsigned int maxpat = 0, i, j, k;
  311.     unsigned char x;
  312.     signed char c[SCANSIZE];      /* look at first SCANSIZE bytes of sample */
  313.     signed char max, min, *p;
  314.     unsigned long offset, len, lenarry[31];
  315.     long sum;
  316.     double ratio, junk;
  317.  
  318.     offset = ftell(ModFile->f);
  319.     fseek(ModFile->f, 42, SEEK_SET);
  320.     for (i = 0; i < s->n; i++) {    /* need to get actual lengths */
  321.         fread(&x, 1, 1, ModFile->f);
  322.         lenarry[i] = 256 * x;
  323.         fread(&x, 1, 1, ModFile->f);
  324.         lenarry[i] += x;
  325.         lenarry[i] *= 2;
  326.         fseek(ModFile->f, 28, SEEK_CUR);
  327.     }
  328.     fseek(ModFile->f, 20 + 30 * s->n, SEEK_SET);
  329.     fread(&x, 1, 1, ModFile->f);
  330.     fread(c, 1, 129, ModFile->f);
  331.     for (; x; x--)
  332.         if (c[x] > maxpat)
  333.             maxpat = c[x];
  334.     len = 1024L * (maxpat + 1) + 30 * s->n + ((s->n > 30) ? 4 : 0) + 150;
  335.     fseek(ModFile->f, len, SEEK_SET);
  336.     for (i = 0, sam = s->s; i < s->n; i++, sam++) {
  337.         sam->t[0] = 0; /* set transposition values to 0 */
  338.         sam->t[1] = 0;
  339.         sam->t[2] = 0;
  340.         sam->a[0] = 0;
  341.         if (!sam->l) {
  342.             sam->a[1] = 1; /* no sample = no volume shifting */
  343.             sam->a[2] = 1;
  344.             fseek(ModFile->f, lenarry[i], SEEK_CUR);
  345.         } else {
  346.             len = min(lenarry[i], SCANSIZE);
  347.             if (!(j = len = fread(c, 1, len, ModFile->f)))
  348.                 len = 1;
  349.             fseek(ModFile->f, lenarry[i] - j, SEEK_CUR);
  350.             min = 127;
  351.             max = -128;
  352.             sum = 0;
  353.             for (p = c; j--; p++) {
  354.                 sum += *p;
  355.                 if (*p > max)
  356.                     max = *p;
  357.                 if (*p < min)
  358.                     min = *p;
  359.             }
  360.             ratio = 1.0 * sum / len;  /* get average .. normally ~0 */
  361.             if (fabs(max - ratio) > fabs(min - ratio))
  362.                 ratio = fabs(min - ratio) / 64.0;
  363.             else
  364.                 ratio = fabs(max - ratio) / 64.0;
  365.             j = k = 1;
  366.             while (k++ < 15)    /* now find decent rational approx. */
  367.                 if (fabs(modf(ratio * k, &junk)-.5) > fabs(modf(ratio * j, &junk)-.5))
  368.                     j = k;
  369.             if (!(sam->a[1] = ratio * j + 0.5)) {
  370.                 sam->a[1] = 1;   /* if really small scaling needed.. */
  371.                 sam->a[2] = 16;
  372.             } else
  373.                 sam->a[2] = j;
  374.         }
  375.     }
  376.     fseek(ModFile->f, offset, SEEK_SET);
  377. }
  378.  
  379. int SetDefaults(samps s, string fn)
  380. /*
  381.  * Returns: NZ if the samples in s have been sucessfully allocated default
  382.  *    values corresponding to definitions in the DEF_MAPFILE file, and from
  383.  *    a .mm file corresponding to the filename fn
  384.  */
  385. {
  386.     FILE *f;
  387.     char i, m[MAXSTRING];
  388.     int d, e[6], v;
  389.     samp *sam;
  390.     string n, t;
  391.     bfile mmf;
  392.  
  393.     t = strchr(strcpy(m, fn), '.');
  394.     if (t==NULL) {
  395.         i = strlen(m);
  396.         m[i] = '.';
  397.     } else
  398.         i = t-m;
  399.     m[++i] = 'm';
  400.     m[++i] = 'm';
  401.     m[++i] = 0;
  402.     mmf->f = NULL;
  403.     if (OpenIn(mmf, m)) {
  404.         for (i = s->n, sam = s->s; i--; sam++) {
  405.             sam->m = InByte(mmf);
  406.             sam->t[0] = (signed char) InByte(mmf);
  407.             sam->t[1] = (signed char) InByte(mmf);
  408.             sam->t[2] = (signed char) InByte(mmf);
  409.             sam->a[0] = (signed char) InByte(mmf);
  410.             sam->a[1] = (signed char) InByte(mmf);
  411.             sam->a[2] = (signed char) InByte(mmf);
  412.         }
  413.         CloseIn(mmf);
  414.     } else
  415.         ScanSamples(s);
  416.     if ((f = fopen(DEF_MAPFILE, "rt"))==NULL) {
  417.         _NOFIL[3] = DEF_MAPFILE;
  418.         InfoBox(_NOFIL);
  419.         return 0;
  420.     }
  421.     i = s->n;
  422.     for (sam = s->s; i--; sam++)
  423.         if (sam->l) {
  424.             n = SimplifyName(sam->n);
  425.             t = GetLine(f);
  426.             e[3] = 0;       /* These are the default volume constants */
  427.             e[4] = 1;
  428.             e[5] = 1;
  429.             sscanf(t, "%s %d %d %d %d %d %d %d", m, &d, &e[0], &e[1], &e[2],
  430.                 &e[3], &e[4], &e[5]);
  431.             if ((v = strcmp(m, n))>0) {
  432.                 rewind(f);
  433.                 free(t);
  434.                 t = GetLine(f);
  435.                 e[3] = 0;
  436.                 e[4] = 1;
  437.                 e[5] = 1;
  438.                 sscanf(t, "%s %d %d %d %d %d %d %d", m, &d, &e[0], &e[1], &e[2],
  439.                     &e[3], &e[4], &e[5]);
  440.                 v = strcmp(m, n);
  441.             }
  442.             free(t);
  443.             while (v < 0 && (t = GetLine(f)) != NULL) {
  444.                 e[3] = 0;
  445.                 e[4] = 1;
  446.                 e[5] = 1;
  447.                 sscanf(t, "%s %d %d %d %d %d %d %d", m, &d, &e[0], &e[1], &e[2],
  448.                     &e[3], &e[4], &e[5]);
  449.                 free(t);
  450.                 v = strcmp(m, n);
  451.             }
  452.             if (!v) {
  453.                 sam->m = d;
  454.                 sam->t[0] = e[0];
  455.                 sam->t[1] = e[1];
  456.                 sam->t[2] = e[2];
  457.                 sam->a[0] = e[3];
  458.                 sam->a[1] = e[4];
  459.                 sam->a[2] = e[5];
  460.             }
  461.             free(n);
  462.         }
  463.     fclose(f);
  464.     return 1;
  465. }
  466.  
  467. void SaveDefaults(samps s, string fn)
  468. /*
  469.  * Post: The samples attributes in s have been written to a .mm file
  470.  *   corresponding to the filename fn
  471.  */
  472. {
  473.     char m[MAXSTRING];
  474.     string t;
  475.     char i;
  476.     bfile mmf;
  477.     samp *sam;
  478.  
  479.     t = strchr(strcpy(m, fn), '.');
  480.     if (t==NULL) {
  481.         i = strlen(m);
  482.         m[i] = '.';
  483.     } else
  484.         i = t-m;
  485.     m[++i] = 'm';
  486.     m[++i] = 'm';
  487.     m[++i] = 0;
  488.     mmf->f = NULL;
  489.     if (!OpenOut(mmf, m))
  490.         return;
  491.     for (i = s->n, sam = s->s; i--; sam++) {
  492.         OutByte(mmf, sam->m);
  493.         OutByte(mmf, sam->t[0]);
  494.         OutByte(mmf, sam->t[1]);
  495.         OutByte(mmf, sam->t[2]);
  496.         OutByte(mmf, sam->a[0]);
  497.         OutByte(mmf, sam->a[1]);
  498.         OutByte(mmf, sam->a[2]);
  499.     }
  500.     CloseOut(mmf);
  501. }
  502.  
  503. void NullArryFree(string *sp)
  504. /* Post: The NULL-terminated array sp is gone */
  505. {
  506.     string *t;
  507.  
  508.     t = sp;
  509.     while (*t != NULL)
  510.         free(*(t++));
  511.     free(sp);
  512. }
  513.  
  514. int Instrument()
  515. /* Returns: MIDI instrument selected from file DEF_INSFILE */
  516. {
  517.     static int w = 0, doff = -1;
  518.     static string *sp = NULL;
  519.     int c;
  520.  
  521.     if (sp == NULL) {
  522.         FILE *f;
  523.         string *t, s;
  524.         int x, i = 1;
  525.  
  526.         if ((f = fopen(DEF_INSFILE, "rt"))==NULL) {
  527.             _NOFIL[3] = DEF_INSFILE;
  528.             InfoBox(_NOFIL);
  529.             return -1;
  530.         }
  531.         if ((t = sp = (string *) malloc(257 * sizeof(string)))==NULL) {
  532.             InfoBox(_OOME);
  533.             return -1;
  534.         }
  535.         while ((s = GetLine(f)) != NULL)
  536.             if (x = strlen(s)) { /* ignore blank lines */
  537.                 i++;
  538.                 if (x > w)
  539.                     w = x;
  540.                 *(t++) = s;
  541.                 if (doff < 0 && *s=='D')  /* take note of first drum position */
  542.                     sscanf(s, "D%d ", &doff);
  543.             }
  544.         *t = NULL;
  545.         sp = (string *) realloc(sp, i * sizeof(string)); /* i <= 257 */
  546.         fclose(f);
  547.     }
  548.     c = ScrollChoice("MIDI Instruments", sp, w);
  549.     if (c>127)
  550.         c += doff;
  551.     return c;
  552. }
  553.  
  554. string MIDIVal(samp *sam)
  555. /* Returns: a string representing the MIDI instrument *sam */
  556. {
  557.     string s;
  558.  
  559.     s = (string) malloc(5);
  560.     if (sam->m < 128)
  561.         sprintf(s, "%4d", sam->m);
  562.     else
  563.         sprintf(s, "D%3d", sam->m - 128);
  564.     return s;
  565. }
  566.  
  567. string TransVal(samp *sam)
  568. /* Returns: a string representing the transpose amount of *sam */
  569. {
  570.     string s;
  571.  
  572.     s = (string) malloc(15);
  573.     if (!sam->t[1])
  574.         sprintf(s, "          %4d", sam->t[0]);
  575.     else if (!sam->t[2])
  576.         sprintf(s, "     %4d,%4d", sam->t[0], sam->t[1]);
  577.     else
  578.         sprintf(s, "%4d,%4d,%4d", sam->t[0], sam->t[1], sam->t[2]);
  579.     return s;
  580. }
  581.  
  582. string VolumeVal(samp *sam)
  583. /* Returns: a string representing the volume shifting formula for *sam */
  584. {
  585.     string s;
  586.  
  587.     s = (string) malloc(17);
  588.     if (sam->a[0] < 0)
  589.         sprintf(s, "(? -%3d)*%3d/%3d", abs(sam->a[0]), sam->a[1], sam->a[2]);
  590.     else
  591.         sprintf(s, "(? +%3d)*%3d/%3d", sam->a[0], sam->a[1], sam->a[2]);
  592.     return s;
  593. }
  594.  
  595. string NullVal(samp *sam)
  596. /* Returns: an empty string */
  597. {
  598.     string s;
  599.  
  600.     *(s = (string) malloc(1)) = 0;
  601.     return s;
  602. }
  603.  
  604. int Sample(samps s, string (*MIDIVal)(samp *x), int w)
  605. /* Returns: Sample selected from list of samples, -1 on error */
  606. {
  607.     string *sp, *t, p;
  608.     samp *sam;
  609.     int i;
  610.  
  611.     if ((t = sp = (string *) malloc((s->n + 1) * sizeof(string)))==NULL) {
  612.         InfoBox(_OOME);
  613.         return -1;
  614.     }
  615.     for (i = s->n, sam = s->s; i--; sam++) {
  616.         *t = (string) malloc(25 + w);
  617.         p = MIDIVal(sam);
  618.         sprintf(*t, "%c%-22s %s", (sam->l) ? '*' : ' ', sam->n, p);
  619.         free(p);
  620.         t++;
  621.     }
  622.     *t = NULL;
  623.     i = ScrollChoice("Select Sample", sp, 24 + w);
  624.     NullArryFree(sp);
  625.     return i;
  626. }
  627.  
  628. int ChooseChannels(samps s)
  629. /*
  630.  * Returns: The number of different channels needed to play instruments. If
  631.  *    that number is not greater than 16, upto 16 channels will be allocated
  632.  *    to the samples.
  633.  */
  634. {
  635.     unsigned char c, d = 0, i[128], m, n, numchan;
  636.     samp *sam1, *sam2;
  637.  
  638.     n = s->n;
  639.     sam1 = s->s;
  640.     memset(i, 0, 128);
  641.     for (n = s->n, sam1 = s->s; n--; sam1++) {
  642.         sam1->c = -1;
  643.         if (sam1->l)
  644.             if (sam1->m > 127) {
  645.                 d = 1;
  646.                 sam1->c = DrumChann;
  647.             } else
  648.                 i[sam1->m] = 1;
  649.         else
  650.             sam1->m = 0;
  651.     }
  652.     for (numchan = d, n = 128; n--; numchan += i[n]);
  653.     if (numchan > 16)
  654.         return numchan;
  655.     /* Ok.. now we must go through and set channels appropriately */
  656.     m = s->n;
  657.     sam1 = s->s;
  658.     c = 0;
  659.     while (m--) {
  660.         if (sam1->c < 0) {
  661.             sam1->c = c;
  662.             n = m;
  663.             sam2 = sam1 + 1;
  664.             while (n--) {
  665.                 if (sam2->c < 0)
  666.                     if (sam2->m == sam1->m || ! sam2->l)
  667.                         sam2->c = c;
  668.                 sam2++;
  669.             }
  670.             if (++c == DrumChann && d)
  671.                 c++;
  672.         }
  673.         sam1++;
  674.     }
  675.     return numchan;
  676. }
  677.  
  678. void MapSamples(samps s)
  679. /* Post: The samples s have been allocated appropriate instruments, we hope */
  680. {
  681.     int i, j;
  682.  
  683.     do
  684.         if ((i = Sample(s, MIDIVal, 4))>=0 && (j = Instrument())>=0)
  685.             s->s[i].m = j;
  686.     while (i>=0);
  687. }
  688.  
  689. void SaveSamp(samp sam)
  690. /* Post: The sample sam has been updated in the DEF_MAPFILE file */
  691. {
  692.     FILE *f1, *f2;
  693.     string s, n;
  694.     int v, d;
  695.     char m[MAXSTRING];
  696.  
  697.     if (!sam.l)
  698.         return;
  699.     if ((f1 = fopen(DEF_MAPFILE, "rt")) == NULL) {
  700.         _NOFIL[3] = DEF_MAPFILE;
  701.         InfoBox(_NOFIL);
  702.         return;
  703.     }
  704.     f2 = fopen("temp.$$$", "wt");
  705.     n = SimplifyName(sam.n);
  706.     v = 1;
  707.     while (v > 0 && (s = GetLine(f1)) != NULL) {
  708.         sscanf(s, "%s %d", m, &d);
  709.         if ((v = strcmp(n, m)) <= 0) {
  710.             fprintf(f2, "%s %d %d %d %d %d %d %d\n", n, sam.m, sam.t[0], sam.t[1],
  711.                 sam.t[2], sam.a[0], sam.a[1], sam.a[2]);
  712.             free(n);
  713.             n = NULL;
  714.             if (v)
  715.                 fprintf(f2, "%s\n", s);
  716.         } else
  717.             fprintf(f2, "%s\n", s);
  718.         free(s);
  719.     }
  720.     while ((s = GetLine(f1))!=NULL) {
  721.         fprintf(f2, "%s\n", s);
  722.         free(s);
  723.     }
  724.     if (n != NULL) {
  725.         fprintf(f2, "%s %d %d %d %d %d %d %d\n", n, sam.m, sam.t[0], sam.t[1],
  726.             sam.t[2], sam.a[0], sam.a[1], sam.a[2]);
  727.         free(n);
  728.     }
  729.     fclose(f2);
  730.     fclose(f1);
  731.     if (unlink(DEF_MAPFILE)<0 || rename("temp.$$$", DEF_MAPFILE)<0)
  732.         InfoBox(_NOSAV);
  733. }
  734.  
  735. void SaveSamples(samps s)
  736. /* Post: The desired sample attributes of s have been saved to disk */
  737. {
  738.     int i;
  739.  
  740.     do
  741.         if ((i = Sample(s, NullVal, 0))>=0)
  742.             SaveSamp(s->s[i]);
  743.     while (i>=0);
  744. }
  745.  
  746. void Transpositions(samps s)
  747. /* Post: All necessary transpositions have been applied to the samples s */
  748. {
  749.     int i;
  750.  
  751.     do
  752.         if ((i = Sample(s, TransVal, 14))>=0) {
  753.             char s1[41];
  754.             string s2;
  755.             samp *sam;
  756.  
  757.             sam = s->s + i;
  758.             if (!sam->t[1])
  759.                 sprintf(s1, "%d", sam->t[0]);
  760.             else if (!sam->t[2])
  761.                 sprintf(s1, "%d,%d", sam->t[0], sam->t[1]);
  762.             else
  763.                 sprintf(s1, "%d,%d,%d", sam->t[0], sam->t[1], sam->t[2]);
  764.             s2 = DialogBox(_TRAQ, s1);
  765.             sam->t[1] = 0;
  766.             sam->t[2] = 0;
  767.             sscanf(s2, "%d,%d,%d", &sam->t[0], &sam->t[1], &sam->t[2]);
  768.             free(s2);
  769.             if ((sam->t[0] || sam->t[1]) && sam->m > 127)
  770.                 InfoBox(_TRANWRN);
  771.             if (sam->t[0]<-128 || sam->t[0]>127 || sam->t[1]<-128 ||
  772.              sam->t[1]>127 || sam->t[2]<-128 || sam->t[2]>127) {
  773.                 InfoBox(_TRANE);
  774.                 sam->t[0] = 0;
  775.                 sam->t[1] = 0;
  776.                 sam->t[2] = 0;
  777.             }
  778.         }
  779.     while (i>=0);
  780. }
  781.  
  782. void VolumeShifts(samps s)
  783. /* Post: All volume shifting formulae have been defined for the samples s */
  784. {
  785.     int i;
  786.  
  787.     do
  788.         if ((i = Sample(s, VolumeVal, 16))>=0) {
  789.             char s1[41];
  790.             string s2;
  791.             samp *sam;
  792.  
  793.             sam = s->s + i;
  794.             sprintf(s1, "%d,%d,%d", sam->a[0], sam->a[1], sam->a[2]);
  795.             s2 = DialogBox(_VSHQ, s1);
  796.             if (sscanf(s2, "%d,%d,%d", &sam->a[0], &sam->a[1], &sam->a[2])!=3 ||
  797.              sam->a[0] > 127 || sam->a[0] < -128 || sam->a[1] > 127 ||
  798.              sam->a[1] < 0 || sam->a[2] > 127 || sam->a[2] < 1) {
  799.                 InfoBox(_VSHE);
  800.                 sam->a[0] = 0;
  801.                 sam->a[1] = 1;
  802.                 sam->a[2] = 1;
  803.             }
  804.             free(s2);
  805.         }
  806.     while (i>=0);
  807. }
  808.  
  809. void SetDrumChannel()
  810. /* Post: Drum channel is set to desired value - 1 */
  811. {
  812.     string s;
  813.     char s2[3];
  814.  
  815.     sprintf(s2, "%d", DrumChann+1);
  816.     s = DialogBox(_DCQ, s2);
  817.     if (sscanf(s, "%d", &DrumChann))
  818.         DrumChann--;        /* Drum channel is stored as value - 1 */
  819. }
  820.  
  821. void SetTempoType()
  822. /* Post: Tempo interpretting will either be extended or normal */
  823. {
  824.     char inp;
  825.  
  826.     while ((inp = InfoBox(TempoType ? _TTQ1 : _TTQ0)) != '0' && inp != '1');
  827.     TempoType = inp - '0';
  828. }
  829.  
  830. unsigned char NoteValue(unsigned int n)
  831. /* Returns: MIDI note equivalent of MOD period-lengths */
  832. {
  833.     static unsigned int t[72] = {
  834.         1712, 1616, 1525, 1440, 1357, 1281, 1209, 1141, 1077, 1017, 961, 907,
  835.         856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
  836.         428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
  837.         214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113,
  838.         107, 101, 95, 90, 85, 80, 76, 71, 67, 64, 60, 57,
  839.         53, 50, 48, 45, 42, 40, 38, 36, 34, 32, 30, 28
  840.         };
  841.     signed char a = 0, m = 35, b = 71;
  842.  
  843.     if (!n)
  844.         return 0;
  845.     for (; b-a > 1; m = (a + b) / 2) /* binary search */
  846.         if (t[m] < n)
  847.             b = m;
  848.         else
  849.             a = m;
  850.     if (n - t[b] < t[a] - n) /* choose closest value */
  851.         return b + 36;
  852.     return a + 36;
  853. }
  854.  
  855. unsigned long NoteLength(unsigned char n, unsigned int l, unsigned int b)
  856. /* Returns: # of ticks to play note length l at pitch n, b beats/min */
  857. {
  858.     static float t[84] = {
  859.         3.200e-3, 3.020e-3, 2.851e-3, 2.691e-3, 2.540e-3, 2.397e-3,
  860.         2.263e-3, 2.136e-3, 2.016e-3, 1.903e-3, 1.796e-3, 1.695e-3,
  861.         1.600e-3, 1.510e-3, 1.425e-3, 1.345e-3, 1.270e-3, 1.197e-3,
  862.         1.131e-3, 1.068e-3, 1.008e-3, 9.514e-4, 8.980e-4, 8.476e-4,
  863.         8.000e-4, 7.551e-4, 7.127e-4, 6.727e-4, 6.350e-4, 5.993e-4,
  864.         5.657e-4, 5.339e-4, 5.040e-4, 4.757e-4, 4.490e-4, 4.238e-4,
  865.         4.000e-4, 3.775e-4, 3.564e-4, 3.364e-4, 3.175e-4, 2.997e-4,
  866.         2.828e-4, 2.670e-4, 2.520e-4, 2.378e-4, 2.245e-4, 2.119e-4,
  867.         2.000e-4, 1.888e-4, 1.782e-4, 1.682e-4, 1.587e-4, 1.498e-4,
  868.         1.414e-4, 1.335e-4, 1.260e-4, 1.189e-4, 1.122e-4, 1.059e-4,
  869.         1.000e-4, 9.439e-5, 8.909e-5, 8.409e-5, 7.937e-5, 7.492e-5,
  870.         7.071e-5, 6.674e-5, 6.300e-5, 5.946e-5, 5.612e-5, 5.297e-5,
  871.         5.000e-5, 4.719e-5, 4.454e-5, 4.204e-5, 3.969e-5, 3.746e-5,
  872.         3.536e-5, 3.337e-5, 3.150e-5, 2.973e-5, 2.806e-5, 2.649e-5
  873.         }; /* multipliers for each pitch: 12th roots of 2 apart */
  874.  
  875.     return t[n - 36] * b * l; /* better not slide out of this range :( */
  876. }
  877.  
  878. void WriteHeader(bfile mf, unsigned char n)
  879. /* Post: The MIDI header has been written to mf, with #tracks = n */
  880. {
  881.     static unsigned char MIDIH[14] = {
  882.         77, 84, 104, 100, 0, 0, 0, 6, 0, 1, 0, -1, 0, 192
  883.     };
  884.     int i = 0;
  885.  
  886.     MIDIH[11] = n+1;
  887.     while (i < 14)
  888.         OutByte(mf, MIDIH[i++]);
  889. }
  890.  
  891. unsigned int Trk0Info(bfile mf, string s)
  892. /*
  893.  * Returns: the number of bytes written as track 0 so far, given mf as the
  894.  *    output MIDI file. s is the name of the tune.
  895.  */
  896. {
  897.     static unsigned char TRK0I[63] = {
  898.         0, 255, 2, 42, 70, 105, 108, 101, 32, 67, 111, 112, 121, 114, 105, 103,
  899.         104, 116, 32, 40, 99, 41, 32, 49 ,57, 57, 51, 32, 65, 100, 114, 101, 110,
  900.         97, 108, 105, 110, 32, 83, 111, 102, 116, 119, 97, 114, 101,
  901.         0, 255, 88, 4, 3, 2, 24, 8,
  902.         0, 255, 89, 2, 0, 0,
  903.         0, 255, 3
  904.         }; /* standard header + copyright message */
  905.     unsigned int i = 0;
  906.  
  907.     while (i < 63)
  908.         OutByte(mf, TRK0I[i++]);
  909.     i = 64 + strlen(s);
  910.     OutByte(mf, strlen(s));
  911.     while (*s)
  912.         OutByte(mf, *(s++));
  913.     return i;
  914. }
  915.  
  916. void AddToLog(unsigned long a, unsigned long b)
  917. /* Post: a (file position) and b (number) have been added to the log */
  918. {
  919.     PosLog[PosI++] = b;
  920.     PosLog[PosI++] = a;
  921. }
  922.  
  923. void WriteLog(FILE *f)
  924. /* Post: PosLog has been written into file f */
  925. {
  926.     unsigned long x, y;
  927.     unsigned char c[4];
  928.  
  929.     if (!PosI)
  930.         return;
  931.     x = ftell(f);
  932.     while (PosI) {
  933.         fseek(f, PosLog[--PosI], SEEK_SET);
  934.         y = PosLog[--PosI];
  935.         c[3] = y & 255;
  936.         y >>= 8;
  937.         c[2] = y & 255;
  938.         y >>= 8;
  939.         c[1] = y & 255;
  940.         y >>= 8;
  941.         c[0] = y;
  942.         fwrite(c, 1, 4, f);
  943.     }
  944.     fseek(f, x, SEEK_SET);
  945. }
  946.  
  947. unsigned char RestrictVol(int v)
  948. /* Returns: A volume in the range 0..127 */
  949. {
  950.     if (v < 0)
  951.         return 0;
  952.     else
  953.         return (v > 127) ? 127 : v;
  954. }
  955.  
  956. void ConvertMOD(bfile f1, bfile f2, string tn, samps smp)
  957. /*
  958.  * Post: The Amiga MODfile f1 has been converted and written to MIDI file f2,
  959.  *    using instrument mappings smp. Tune has a name tn.
  960.  */
  961. {
  962.     unsigned char b, length, pattern[128];
  963.     string n, t;
  964.     unsigned int i, j, k, tempdone = 0;
  965.     samp *sam;
  966.     struct bpos p1, p2;
  967.  
  968.     p1 = FPos(f1);
  969.     length = InByte(f1);
  970.     InByte(f1);
  971.     for (i = 0; i < 128; pattern[i++] = InByte(f1));
  972.     Inskipp(f1, (smp->n > 15) ? 4 : 0);
  973.     WriteHeader(f2, smp->n);
  974.     p2 = FPos(f1);
  975.     for (i = 0, sam = smp->s - 1; i <= smp->n; sam++, i++) {
  976.         unsigned long cnt, inst, timer, delay[4];
  977.         unsigned char c;
  978.  
  979.         OutByte(f2, 77);
  980.         OutByte(f2, 84);
  981.         OutByte(f2, 114);
  982.         OutByte(f2, 107);
  983.         inst = Beatle(f2); /* store this position so can set AFTERWARDS - hehe */
  984.         OutByte(f2, 0);
  985.         OutByte(f2, 0);
  986.         OutByte(f2, 0);
  987.         OutByte(f2, 0);
  988.         if (!i)
  989.             cnt = Trk0Info(f2, tn);
  990.         else {
  991.             OutByte(f2, 0);
  992.             OutByte(f2, 255);
  993.             OutByte(f2, 3);
  994.             b = strlen(n = sam->n);
  995.             OutByte(f2, b);
  996.             cnt = 7 + b;
  997.             while (b--)
  998.                 OutByte(f2, *(n++));
  999.             c = sam->c;
  1000.             OutByte(f2, 0);
  1001.             OutByte(f2, 0xC0 + c); /* set channel */
  1002.             OutByte(f2, (sam->m > 127) ? 126 : sam->m);
  1003.         }
  1004.         timer = 0;
  1005.         if (sam->l || !i) {
  1006.             unsigned int bpm, ticks, l, effect, h;
  1007.             unsigned char sampnum, lastsam[4] = {0}, vol[4];
  1008.             signed char patbreak;
  1009.             unsigned long x, pause;
  1010.             int note, lastslide, slideto;
  1011.             int n[4][48][2]; /* note data for a song position */
  1012.             int lastn[4]; /* last note on a particular channel */
  1013.  
  1014.             sprintf(_CNVPOS[2]+7, "%d", i); /* some compilers hate this */
  1015.             DrawBox(_CNVPOS);
  1016.             memset(lastn, 0, 4 * sizeof(int));
  1017.             memset(vol, 0, 4);
  1018.             memset(delay, 0, 4 * sizeof(long));
  1019.             bpm = 125;
  1020.             ticks = 6;
  1021.             lastslide = slideto = 0;
  1022.             patbreak = 0;
  1023.             for (h = 48; h--; )
  1024.                 for (k = 4; k--; )
  1025.                     n[k][h][0] = 0;
  1026.             for (l = 0; l < length; l++) {
  1027.                 if (pattern[l]<=pattern[l-1] || !l) {
  1028.                     FGoto(f1, p2);
  1029.                     x = (unsigned) 1024 * pattern[l];
  1030.                 } else
  1031.                     x = (unsigned) 1024 * (pattern[l]-pattern[l-1]-1);
  1032.                 Inskipp(f1, x);
  1033.                 j = 64;
  1034.                 if (patbreak>0)
  1035.                     patbreak = 1-patbreak;
  1036.                 while (j--) {
  1037.                     pause = 0;
  1038.                     if (patbreak)
  1039.                         Inskipp(f1, 16);
  1040.                     else
  1041.                         for (k = 0; k < 4; k++) {
  1042.                             n[k][0][1] = sam->v;
  1043.                             sampnum = InByte(f1);
  1044.                             note = ((sampnum & 15) << 8) + InByte(f1);
  1045.                             effect = InByte(f1);
  1046.                             sampnum = (sampnum & ~15) + (effect >> 4);
  1047.                             if (!i)
  1048.                                 note = 0;
  1049.                             if ((note || sampnum) && delay[k]) { /* stop old note */
  1050.                                 cnt += 3 + WriteVLQ(f2, timer);
  1051.                                 timer = 0;
  1052.                                 OutByte(f2, 0x80 + c); /* note off */
  1053.                                 OutByte(f2, ENOTE(lastn[k], 0));
  1054.                                 OutByte(f2, RestrictVol(EVOL(vol[k])));
  1055.                                 if (sam->t[1]) {
  1056.                                     cnt += 4;
  1057.                                     OutByte(f2, 0);
  1058.                                     OutByte(f2, 0x80 + c);
  1059.                                     OutByte(f2, ENOTE(lastn[k], 1));
  1060.                                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1061.                                     if (sam->t[2]) {
  1062.                                         cnt += 4;
  1063.                                         OutByte(f2, 0);
  1064.                                         OutByte(f2, 0x80 + c);
  1065.                                         OutByte(f2, ENOTE(lastn[k], 2));
  1066.                                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1067.                                     }
  1068.                                 }
  1069.                                 delay[k] = 0;
  1070.                             }
  1071.                             if (!note && sampnum == i) /* check "defaults" */
  1072.                                 note = lastn[k];
  1073.                             else if (!sampnum)
  1074.                                 if (lastsam[k] == i)
  1075.                                     sampnum = i;
  1076.                                 else
  1077.                                     note = 0;
  1078.                             else {
  1079.                                 if (sampnum != i)
  1080.                                     note = 0;
  1081.                                 lastsam[k] = sampnum;
  1082.                             }
  1083.                             n[k][0][0] = note;
  1084.                             effect = ((effect & 15) << 8) + InByte(f1);
  1085.                             switch (effect & 0xF00) {
  1086.                                 case 0x000: { /* arpeggio */
  1087.                                     int nv;
  1088.  
  1089.                                     if (!i || !effect || sam->m > 127)
  1090.                                         break;
  1091.                                     if (!note)
  1092.                                         if (!delay[k])
  1093.                                             break;
  1094.                                         else {
  1095.                                             nv = NoteValue(lastn[k]);
  1096.                                             n[k][47][0] = lastn[k];
  1097.                                             n[k][47][1] = vol[k];
  1098.                                             if (effect & 0x0F0)
  1099.                                                 n[k][16][0] = -(nv + ((effect & 0x0F0) >> 4));
  1100.                                             n[k][16][1] = vol[k];
  1101.                                             if (effect & 0x00F)
  1102.                                                 n[k][32][0] = -(nv + (effect & 0x00F));
  1103.                                             n[k][32][1] = vol[k];
  1104.                                         }
  1105.                                     else {
  1106.                                         nv = NoteValue(note);
  1107.                                         n[k][47][0] = note;
  1108.                                         n[k][47][1] = sam->v;
  1109.                                         if (effect & 0x0F0)
  1110.                                             n[k][16][0] = -(nv + ((effect & 0x0F0) >> 4));
  1111.                                         n[k][16][1] = sam->v;
  1112.                                         if (effect & 0x00F)
  1113.                                             n[k][32][0] = -(nv + (effect & 0x00F));
  1114.                                         n[k][32][1] = sam->v;
  1115.                                     }
  1116.                                     break;
  1117.                                     }
  1118.                                 case 0x300: /* slide to */
  1119.                                     if (!note && !slideto || note==lastn[k] || sam->m > 127)
  1120.                                         break;
  1121.                                     if (effect & 0x0FF)
  1122.                                         lastslide = effect & 0x0FF;
  1123.                                     else
  1124.                                         lastslide = abs(lastslide);
  1125.                                     if (note)
  1126.                                         slideto = note;
  1127.                                     if (slideto > lastn[k]) {
  1128.                                         n[k][0][0] = lastn[k] + lastslide*(ticks-1);
  1129.                                         if (n[k][0][0] > 856)
  1130.                                             n[k][0][0] = 856;
  1131.                                         if (n[k][0][0] > slideto)
  1132.                                             n[k][0][0] = slideto;
  1133.                                     } else {
  1134.                                         n[k][0][0] = lastn[k] - lastslide*(ticks-1);
  1135.                                         if (n[k][0][0] < 113)
  1136.                                             n[k][0][0] = 113;
  1137.                                         if (n[k][0][0] < slideto)
  1138.                                             n[k][0][0] = slideto;
  1139.                                     }
  1140.                                     n[k][0][1] = vol[k];
  1141.                                     break;
  1142.                                 case 0x100: /* slide up */
  1143.                                 case 0x200: /* slide down */
  1144.                                     if (!(effect & 0x0FF) || sam->m > 127)
  1145.                                         break;
  1146.                                     if (effect & 0x200)
  1147.                                         lastslide = effect & 0x0FF;
  1148.                                     else
  1149.                                         lastslide = -(effect & 0x0FF);
  1150.                                     if (!note)
  1151.                                         if (!delay[k])
  1152.                                             break;
  1153.                                         else {
  1154.                                             n[k][0][0] = lastn[k] + lastslide;
  1155.                                             n[k][0][1] = vol[k];
  1156.                                         }
  1157.                                     else
  1158.                                         n[k][0][0] += lastslide;
  1159.                                     if (n[k][0][0] > 856)
  1160.                                         n[k][0][0] = 856;
  1161.                                     else if (n[k][0][0] < 113)
  1162.                                         n[k][0][0] = 113;
  1163.                                     break;
  1164.                                 case 0x400: /* vibrato */
  1165.                                 case 0x700: /* tremolo */
  1166.                                     /* ignore these effects.. not convertable */
  1167.                                     break;
  1168.                                 case 0x500: /* slide to & volume slide */
  1169.                                     if ((note || slideto) && note!=lastn[k] && sam->m < 128) {
  1170.                                         if (note)
  1171.                                             slideto = note;
  1172.                                         if (slideto > lastn[k]) {
  1173.                                             n[k][0][0] = lastn[k] + lastslide*(ticks-1);
  1174.                                             if (n[k][0][0] > 856)
  1175.                                                 n[k][0][0] = 856;
  1176.                                             if (n[k][0][0] > slideto)
  1177.                                                 n[k][0][0] = slideto;
  1178.                                         } else {
  1179.                                             n[k][0][0] = lastn[k] - lastslide*(ticks-1);
  1180.                                             if (n[k][0][0] < 113)
  1181.                                                 n[k][0][0] = 113;
  1182.                                             if (n[k][0][0] < slideto)
  1183.                                                 n[k][0][0] = slideto;
  1184.                                         }
  1185.                                     } else
  1186.                                         n[k][0][0] = 0;
  1187.                                     note = 0;
  1188.                                 case 0x600: /* vibrato & volume slide */
  1189.                                 case 0xA00: { /* volume slide */
  1190.                                     int v;
  1191.  
  1192.                                     if (!note)
  1193.                                         v = vol[k];
  1194.                                     else
  1195.                                         v = sam->v;
  1196.                                     v += (ticks-1)*(effect & 0x0F0); /* can't really slide */
  1197.                                     v -= (ticks-1)*(effect & 0x00F);
  1198.                                     if (v > 127)
  1199.                                         v = 127;
  1200.                                     else if (v < 0)
  1201.                                         v = 0;
  1202.                                     n[k][0][1] = v;
  1203.                                     break;
  1204.                                     }
  1205.                                 case 0x900: /* set offset: pretend it's retrigger */
  1206.                                     if ((!n[k][0][0] || !sampnum) && delay[k]) {
  1207.                                         n[k][0][0] = lastn[k];
  1208.                                         n[k][0][1] = vol[k];
  1209.                                     }
  1210.                                     break;
  1211.                                 case 0xB00: /* position jump: ignore, but break anyway */
  1212.                                     patbreak = 1;
  1213.                                     break;
  1214.                                 case 0xD00: /* pattern break */
  1215.                                     patbreak = 1 + 10 * (effect & 0x0F0) + (effect & 0x00F);
  1216.                                     break;
  1217.                                 case 0xC00: /* set volume */
  1218.                                     n[k][0][1] = effect & 0x0FF;
  1219.                                     break;
  1220.                                 case 0xF00: { /* set tempo */
  1221.                                     int temp;
  1222.  
  1223.                                     temp = effect & 0x0FF;
  1224.                                     if (!temp)
  1225.                                         temp = 1;
  1226.                                     if (temp < 32) {
  1227.                                         ticks = temp;
  1228.                                         if (TempoType) {  /* Tempos act strangely so .. */
  1229.                                             bpm = 750 / temp;
  1230.                                             x = 80000 * temp;
  1231.                                         }
  1232.                                     } else {
  1233.                                         bpm = temp;
  1234.                                         x = 60000000 / temp;
  1235.                                     }
  1236.                                     if (i)
  1237.                                         break; /* only write tempo on track 0 */
  1238.                                     cnt += 6 + WriteVLQ(f2, timer);
  1239.                                     timer = 0;
  1240.                                     OutByte(f2, 255); /* meta-event */
  1241.                                     OutByte(f2, 81); /* set tempo */
  1242.                                     OutByte(f2, 3);
  1243.                                     OutByte(f2, x >> 16);
  1244.                                     OutByte(f2, (x >> 8) & 0xFF);
  1245.                                     OutByte(f2, x & 0xFF);
  1246.                                     tempdone = 1;
  1247.                                     break;
  1248.                                     }
  1249.                                 case 0xE00: /* extended effects */
  1250.                                     switch (effect & 0x0F0) {
  1251.                                         case 0x010: /* fine slide up */
  1252.                                             if (!(effect & 0x00F) || sam->m > 127)
  1253.                                                 break;
  1254.                                             if (!note)
  1255.                                                 if (!delay[k])
  1256.                                                     break;
  1257.                                                 else {
  1258.                                                     n[k][h][0] = lastn[k] + (effect & 0x00F);
  1259.                                                     n[k][h][1] = vol[k];
  1260.                                                 }
  1261.                                             else
  1262.                                                 n[k][h][0] += effect & 0x00F;
  1263.                                             break;
  1264.                                         case 0x020: /* fine slide down */
  1265.                                             if (!(effect & 0x00F) || sam->m > 127)
  1266.                                                 break;
  1267.                                             if (!note)
  1268.                                                 if (!delay[k])
  1269.                                                     break;
  1270.                                                 else {
  1271.                                                     n[k][h][0] = lastn[k] - (effect & 0x00F);
  1272.                                                     n[k][h][1] = vol[k];
  1273.                                                 }
  1274.                                             else
  1275.                                                 n[k][h][0] -= effect & 0x00F;
  1276.                                             break;
  1277.                                         case 0x000: /* set filter on/off */
  1278.                                         case 0x030: /* glissando on/off */
  1279.                                         case 0x040: /* set vibrato wave */
  1280.                                         case 0x050: /* set finetune */
  1281.                                         case 0x060: /* pattern loop */
  1282.                                         case 0x070: /* set tremolo wave */
  1283.                                         case 0x080: /* un-used */
  1284.                                         case 0x0F0: /* invert loop */
  1285.                                             /* can't do these in MIDI.. ignore */
  1286.                                             break;
  1287.                                         case 0x0A0: /* fine volume slide up */
  1288.                                         case 0x0B0: { /* fine volume slide down */
  1289.                                             int v;
  1290.  
  1291.                                             v = sam->v;
  1292.                                             if (effect & 0x0A0)
  1293.                                                 v += effect & 0x00F;
  1294.                                             else
  1295.                                                 v -= effect & 0x00F;
  1296.                                             if (v<0)
  1297.                                                 v = 0;
  1298.                                             else if (v>127)
  1299.                                                 v = 127;
  1300.                                             n[k][0][1] = v;
  1301.                                             break;
  1302.                                             }
  1303.                                         case 0x090: { /* retrigger sample */
  1304.                                             int a, b, c;
  1305.  
  1306.                                             if (!note && !delay[k] || !(effect & 0x00F))
  1307.                                                 break;
  1308.                                             a = effect & 0x00F;
  1309.                                             if (!(ticks / a))
  1310.                                                 break;
  1311.                                             if (!note) {
  1312.                                                 n[k][0][0] = lastn[k];
  1313.                                                 n[k][0][1] = vol[k];
  1314.                                             }
  1315.                                             c = 0;
  1316.                                             b = 1;
  1317.                                             a *= 48;
  1318.                                             while (c < 48) {
  1319.                                                 n[k][c][0] = note;
  1320.                                                 n[k][c][1] = n[k][0][1];
  1321.                                                 c = b * a / ticks;
  1322.                                                 b++;
  1323.                                             }
  1324.                                             break;
  1325.                                             }
  1326.                                         case 0x0C0: { /* cut sample */
  1327.                                             int a;
  1328.  
  1329.                                             if (!note && !delay[k])
  1330.                                                 break;
  1331.                                             a = 48 * (effect & 0x00F) / ticks;
  1332.                                             if (a > 47)
  1333.                                                 break;
  1334.                                             if (note)
  1335.                                                 n[k][a][0] = note;
  1336.                                             else
  1337.                                                 n[k][a][0] = lastn[k];
  1338.                                             n[k][a][1] = 0;
  1339.                                             break;
  1340.                                             }
  1341.                                         case 0x0D0: { /* delay sample */
  1342.                                             int a;
  1343.  
  1344.                                             if (!note || !(effect & 0x00F))
  1345.                                                 break;
  1346.                                             a = 48 * (effect & 0x00F) / ticks;
  1347.                                             n[k][0][0] = 0;
  1348.                                             if (a > 47)
  1349.                                                 break;
  1350.                                             n[k][a][0] = note;
  1351.                                             n[k][a][1] = n[k][a][0];
  1352.                                             break;
  1353.                                             }
  1354.                                         case 0x0E0: /* pattern pause */
  1355.                                             pause = 48 * (effect & 0x00F);
  1356.                                             break;
  1357.                                     }
  1358.                                     break;
  1359.                                 /* else dunno what it does.. disbelieve it ;) */
  1360.                             }
  1361.                         }
  1362.                     for (h = 0; h<48; h++) {
  1363.                         for (k = 0; k < 4; k++)
  1364.                             if (n[k][h][0]) {
  1365.                                 if (delay[k]) { /* turn off old note on same channel */
  1366.                                     cnt += 3 + WriteVLQ(f2, timer);
  1367.                                     timer = 0;
  1368.                                     OutByte(f2, 0x80 + c); /* note off */
  1369.                                     OutByte(f2, ENOTE(lastn[k], 0));
  1370.                                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1371.                                     if (sam->t[1]) {
  1372.                                         cnt += 4;
  1373.                                         OutByte(f2, 0);
  1374.                                         OutByte(f2, 0x80 + c);
  1375.                                         OutByte(f2, ENOTE(lastn[k], 1));
  1376.                                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1377.                                         if (sam->t[2]) {
  1378.                                             cnt += 4;
  1379.                                             OutByte(f2, 0);
  1380.                                             OutByte(f2, 0x80 + c);
  1381.                                             OutByte(f2, ENOTE(lastn[k], 2));
  1382.                                             OutByte(f2, RestrictVol(EVOL(vol[k])));
  1383.                                         }
  1384.                                     }
  1385.                                     delay[k] = 0;
  1386.                                 }
  1387.                                 lastn[k] = n[k][h][0];
  1388.                                 n[k][h][0] = 0;
  1389.                                 vol[k] = n[k][h][1];
  1390.                                 cnt += 3 + WriteVLQ(f2, timer);
  1391.                                 timer = 0;
  1392.                                 OutByte(f2, 0x90 + c); /* note on */
  1393.                                 OutByte(f2, ENOTE(lastn[k], 0));
  1394.                                 OutByte(f2, RestrictVol(EVOL(vol[k])));
  1395.                                 if (sam->t[1]) {
  1396.                                     cnt += 4;
  1397.                                     OutByte(f2, 0);
  1398.                                     OutByte(f2, 0x90 + c);
  1399.                                     OutByte(f2, ENOTE(lastn[k], 1));
  1400.                                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1401.                                     if (sam->t[2]) {
  1402.                                         cnt += 4;
  1403.                                         OutByte(f2, 0);
  1404.                                         OutByte(f2, 0x90 + c);
  1405.                                         OutByte(f2, ENOTE(lastn[k], 2));
  1406.                                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1407.                                     }
  1408.                                 }
  1409.                                 delay[k] = NoteLength(ANOTE(lastn[k]), sam->l, bpm);
  1410.                             } else if (delay[k]==1) {
  1411.                                 delay[k] = 0;
  1412.                                 cnt += 3 + WriteVLQ(f2, timer);
  1413.                                 timer = 0;
  1414.                                 OutByte(f2, 0x80 + c); /* note off */
  1415.                                 OutByte(f2, ENOTE(lastn[k], 0));
  1416.                                 OutByte(f2, RestrictVol(EVOL(vol[k])));
  1417.                                 if (sam->t[1]) {
  1418.                                     cnt += 4;
  1419.                                     OutByte(f2, 0);
  1420.                                     OutByte(f2, 0x80 + c);
  1421.                                     OutByte(f2, ENOTE(lastn[k], 1));
  1422.                                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1423.                                     if (sam->t[2]) {
  1424.                                         cnt += 4;
  1425.                                         OutByte(f2, 0);
  1426.                                         OutByte(f2, 0x80 + c);
  1427.                                         OutByte(f2, ENOTE(lastn[k], 2));
  1428.                                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1429.                                     }
  1430.                                 }
  1431.                             } else if (delay[k]>0)
  1432.                                 delay[k]--;
  1433.                         timer++;
  1434.                     }
  1435.                     timer += pause;
  1436.                     if (patbreak<0)
  1437.                         patbreak++;
  1438.                     else if (patbreak>0) {
  1439.                         patbreak = 1 - patbreak;
  1440.                         while (j) {
  1441.                             j--;
  1442.                             Inskipp(f1, 16); /* 16 bytes / song position */
  1443.                         }
  1444.                     }
  1445.                 }
  1446.             }
  1447.             for (k = 0; k < 4; k++)
  1448.                 if (delay[k]) {
  1449.                     cnt += 3 + WriteVLQ(f2, timer);
  1450.                     timer = 0;
  1451.                     OutByte(f2, 0x80 + c); /* note off */
  1452.                     OutByte(f2, ENOTE(lastn[k], 0));
  1453.                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1454.                     if (sam->t[1]) {
  1455.                         cnt += 4;
  1456.                         OutByte(f2, 0);
  1457.                         OutByte(f2, 0x80 + c);
  1458.                         OutByte(f2, ENOTE(lastn[k], 1));
  1459.                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1460.                         if (sam->t[2]) {
  1461.                             cnt += 4;
  1462.                             OutByte(f2, 0);
  1463.                             OutByte(f2, 0x80 + c);
  1464.                             OutByte(f2, ENOTE(lastn[k], 2));
  1465.                             OutByte(f2, RestrictVol(EVOL(vol[k])));
  1466.                         }
  1467.                     }
  1468.                 }
  1469.         }
  1470.         if (!i && !tempdone) {
  1471.             cnt += 7;
  1472.             OutByte(f2, 0); /* give the default 128 bpm if none done yet */
  1473.             OutByte(f2, 255);
  1474.             OutByte(f2, 81);
  1475.             OutByte(f2, 3);
  1476.             OutByte(f2, 7);
  1477.             OutByte(f2, 39);
  1478.             OutByte(f2, 14);
  1479.         }
  1480.         cnt += 3 + WriteVLQ(f2, timer);
  1481.         OutByte(f2, 255);
  1482.         OutByte(f2, 47);
  1483.         OutByte(f2, 0);
  1484.         AddToLog(inst, cnt); /* process this later */
  1485.         FGoto(f1, p2);
  1486.     }
  1487.     ClearWin();
  1488.     FlushOut(f2);
  1489.     WriteLog(f2->f);
  1490.     FGoto(f1, p1);
  1491. }
  1492.  
  1493. int main(int argc, char *argv[])
  1494. {
  1495.     int c;
  1496.  
  1497.     MainWindow("MIDIMOD - Amiga Noise/Sound/Protracker to MIDI converter",
  1498.         4, "File", 'f', "Samples", 's', "Options", 'o', "Help", 'h');
  1499.     SetMenu(0, 6, "Dest. midi",'d',0, "Source mod",'s',1, "------------",-1,-1,
  1500.         "Convert",'c',2, "------------",-1,-1, "Quit",'q',99);
  1501.     SetMenu(1, 5, "Map samples",'m',3, "Transposing",'t',6, "Volume shift",'v',9,
  1502.         "------------",-1,-1, "Save info",'s',4);
  1503.     SetMenu(2, 2, "Drum channel",'d',7, "Tempo type",'t',8);
  1504.     SetMenu(3, 1, "About",'a',5);
  1505.     MidFile->f = ModFile->f = NULL;
  1506.     MidFN = ModFN = NULL;
  1507.     if (argc>2) {
  1508.         strcpy(MidFN = (string) malloc(strlen(argv[2])+1), argv[2]);
  1509.         if (fclose(fopen(MidFN, "r"))==EOF || tolower(InfoBox(_OUTE))=='y')
  1510.             OpenOut(MidFile, MidFN);
  1511.         if (MidFile->f==NULL) {
  1512.             free(MidFN);
  1513.             MidFN = NULL;
  1514.         }
  1515.     }
  1516.     if (argc>1) {
  1517.         strcpy(ModFN = (string) malloc(strlen(argv[1])+1), argv[1]);
  1518.         if (OpenIn(ModFile, ModFN))
  1519.             if (!ReadModSpecs(ModFile, SongName, Samples))
  1520.                 InfoBox(_MODE);
  1521.             else if (!SetDefaults(Samples, ModFN))
  1522.                 CloseIn(ModFile);
  1523.         if (ModFile->f==NULL) {
  1524.             free(ModFN);
  1525.             ModFN = NULL;
  1526.         }
  1527.     }
  1528.     do {
  1529.         c = Choice();
  1530.         switch(c) {
  1531.             case 0:
  1532.                 MidFN = InitFile(MidFile, MidFN, "MIDI file below.", 0);
  1533.                 break;
  1534.             case 1:
  1535.                 if (ModFile->f != NULL)
  1536.                     SaveDefaults(Samples, ModFN); /* make sure defaults are saved */
  1537.                 ModFN = InitFile(ModFile, ModFN, "MOD file below.", 1);
  1538.                 if (ModFN != NULL)
  1539.                     if (!ReadModSpecs(ModFile, SongName, Samples))
  1540.                         InfoBox(_MODE);
  1541.                     else if (!SetDefaults(Samples, ModFN))
  1542.                         CloseIn(ModFile);
  1543.                 break;
  1544.             case 2:
  1545.                 if (MidFile->f != NULL && ModFile->f != NULL)
  1546.                     if (ChooseChannels(Samples) <= 16)
  1547.                         ConvertMOD(ModFile, MidFile, SongName, Samples);
  1548.                     else
  1549.                         InfoBox(_TMC);
  1550.                 else
  1551.                     InfoBox(_CNVE);
  1552.                 break;
  1553.             case 3:
  1554.                 if (ModFile->f != NULL)
  1555.                     MapSamples(Samples);
  1556.                 else
  1557.                     InfoBox(_MODM);
  1558.                 break;
  1559.             case 4:
  1560.                 if (ModFile->f != NULL)
  1561.                     SaveSamples(Samples);
  1562.                 else
  1563.                     InfoBox(_MODM);
  1564.                 break;
  1565.             case 5:
  1566.                 InfoBox(_ABOUT);
  1567.                 break;
  1568.             case 6:
  1569.                 if (ModFile->f != NULL)
  1570.                     Transpositions(Samples);
  1571.                 else
  1572.                     InfoBox(_MODM);
  1573.                 break;
  1574.             case 7:
  1575.                 SetDrumChannel();
  1576.                 break;
  1577.             case 8:
  1578.                 SetTempoType();
  1579.                 break;
  1580.             case 9:
  1581.                 if (ModFile->f != NULL)
  1582.                     VolumeShifts(Samples);
  1583.                 else
  1584.                     InfoBox(_MODM);
  1585.                 break;
  1586.         }
  1587.     } while (c != 99);
  1588.     if (ModFile->f != NULL) {
  1589.         CloseIn(ModFile);
  1590.         SaveDefaults(Samples, ModFN);
  1591.     }
  1592.     free(ModFN);
  1593.     if (MidFile->f != NULL)
  1594.         CloseOut(MidFile);
  1595.     free(MidFN);
  1596.     EndWindows();
  1597.     return 0;
  1598. }
  1599.